﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

/// <summary>
/// Allows response with various HTTP status codes.
/// </summary>
public class StatusCodeResponse
{
    private HttpContext _context = null;

    public StatusCodeResponse(HttpContext context)
    {
        this._context = context;
    }

    #region Error Responses

    #region Respond 502

    /// <summary>
    /// Throws a 502 http exception.
    /// </summary>
    /// <param name="message">Message to display.</param>
    public void RespondInternalError(string message)
    {
        RespondInternalError(message, null);
    }

    /// <summary>
    /// Responds with a 502 http exception and displays a specified page.
    /// </summary>
    /// <param name="message">Message to display.</param>
    /// <param name="transfer">Page to transfer to.</param>
    public void RespondInternalError(string message, string transfer)
    {
        RespondStatusCode(this._context, System.Net.HttpStatusCode.InternalServerError, message, transfer);
    }

    #endregion

    #region Respond 404
    /// <summary>
    /// Throws a 404 http exception.
    /// </summary>
    /// <param name="message">Message to display.</param>
    public void RespondNotFound(string message)
    {
        RespondNotFound(message, null);
    }

    /// <summary>
    /// Responds with a 404 http exception and displays a specified page.
    /// </summary>
    /// <param name="message">Message to display.</param>
    /// <param name="transfer">Page to transfer to.</param>
    public void RespondNotFound(string message, string transfer)
    {
        RespondStatusCode(this._context, System.Net.HttpStatusCode.NotFound, message, transfer);
    }

    #endregion

    #region Respond 403

    /// <summary>
    /// Throws a 403 http exception.
    /// </summary>
    /// <param name="message">Message to display.</param>
    public void RespondForbidden(string message)
    {
        RespondForbidden(message, null);
    }

    /// <summary>
    /// Responds with a 403 http exception and displays a specified page.
    /// </summary>
    /// <param name="message">Message to display.</param>
    /// <param name="transfer">Page to transfer to.</param>
    public void RespondForbidden(string message, string transfer)
    {
        RespondStatusCode(this._context, System.Net.HttpStatusCode.Forbidden, message, transfer);
    }

    #endregion

    #region Respond 301

    /// <summary>
    /// Throws a 301 http exception.
    /// </summary>
    /// <param name="message">Message to display.</param>
    public void RespondMovedPermanently(string message)
    {
        RespondMovedPermanently(message, null);
    }

    /// <summary>
    /// Responds with a 301 http exception and displays a specified page.
    /// </summary>
    /// <param name="message">Message to display.</param>
    /// <param name="transfer">Page to transfer to.</param>
    public void RespondMovedPermanently(string message, string transfer)
    {
        RespondStatusCode(this._context, System.Net.HttpStatusCode.MovedPermanently, message, transfer);
    }

    #endregion

    /// <summary>
    /// Responds with a specified status code, and if specified - transfers to a page.
    /// </summary>
    /// <param name="status">HttpStatusCode to throw.</param>
    /// <param name="message">Message to throw.</param>
    /// <param name="transfer">Optional transfer page.</param>
    public void RespondStatusCode(System.Net.HttpStatusCode status, string message, string transfer)
    {
        RespondStatusCode(this._context, status, message, transfer);
    }

    /// <summary>
    /// Responds with a specified status code, and if specified - transfers to a page.
    /// </summary>
    /// <param name="context">Current HttpContext.</param>
    /// <param name="status">HttpStatusCode to throw.</param>
    /// <param name="message">Message to throw.</param>
    /// <param name="transfer">Optional transfer page.</param>
    private void RespondStatusCode(HttpContext context, System.Net.HttpStatusCode status, string message, string transfer)
    {
        if (string.IsNullOrEmpty(transfer))
        {
            throw new HttpException((int)status, message);
        }

        context.Response.StatusCode = (int)status;
        context.Response.StatusDescription = message;
        context.Server.Transfer(transfer);
    }

    #endregion
}
